﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Acms.Bll;
using System.Web.UI;

namespace Acms.AcmsControls.ContentControls.Admin
{
    public interface IBaseAdminDisplay
    {
        void LoadData(PostControl PostControl, Post Post);

        void DataBind();
    }

    public abstract class BaseAdminDisplay<T> : CompositeControl, IBaseAdminDisplay where T : BaseContentControlProperties, new()
    {
        public BaseAdminDisplay()
        {
            base.CssClass = GetCssClass;
        }

        protected int PostId { get; private set; }
        protected int TemplateControlId { get; private set; }
        protected int SiteId { get; private set; }
        protected DateTime? SiteVersion { get; private set; }

        protected T Properties { get; private set; }

        private bool dataLoaded = false;
        protected void EnsureDataLoaded()
        {
            if (!dataLoaded)
            {
                LoadData(GetControl());
            }
        }

        protected virtual void LoadData(PostControl control)
        {
            if (control != null)
            {
                try
                {
                    EnsureChildControls();
                    Properties = LoadData(control.Value);
                    if (Properties != null)
                    {
                        PropertiesLoaded(Properties);
                    }
                }
                catch (System.Xml.XmlException e)
                {
                    throw new Acms.Exceptions.ContentControlException(string.Format("Failed to load Xml Data for PostControlId '{0}' : {1}", control.Id, e.Message));
                }
                dataLoaded = true;
            }
        }

        public virtual void PropertiesLoaded(T properties)
        {
        }


        public virtual void LoadData(PostControl control, Post post)
        {
            PostId = post.Id;
            TemplateControlId = control.TemplateControlId;
            SiteId = post.SiteId;
            SiteVersion = post.SiteVersion;
            LoadData(control);
        }

        protected T LoadData(System.Xml.XmlDocument xmlDocument)
        {
            if (xmlDocument != null)
            {
                try
                {
                    System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(T));
                    return (T)xmls.Deserialize(new System.IO.StringReader(xmlDocument.OuterXml));
                }
                catch
                {
                    T tmp = new T();
                    tmp.ReadXml(System.Xml.XmlReader.Create(new System.IO.StringReader(xmlDocument.OuterXml)));
                    return tmp;
                }
            }
            else
            {
                return null;
            }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        protected abstract string GetCssClass {get;}

        protected override object SaveViewState()
        {
            ViewState["PostId"] = PostId;
            ViewState["TemplateControlId"] = TemplateControlId;
            ViewState["SiteId"] = SiteId;
            ViewState["SiteVersion"] = SiteVersion;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if (ViewState["PostId"] is int)
            {
                PostId = (int)ViewState["PostId"];
            }
            if (ViewState["TemplateControlId"] is int)
            {
                TemplateControlId = (int)ViewState["TemplateControlId"];
            }
            if (ViewState["SiteId"] is int)
            {
                SiteId = (int)ViewState["SiteId"];
            }
            SiteVersion = ViewState["SiteVersion"] as DateTime?;
        }

        protected PostControl GetControl()
        {
            return PostControl.GetByPostIdAndTemplateControlId(PostId, TemplateControlId);
        }

        protected void ForceSave(PostControl control, BaseContentControlProperties properties)
        {
            if (control != null)
            {
                System.Xml.Serialization.XmlSerializer xmls = new System.Xml.Serialization.XmlSerializer(typeof(T));
                System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(memStream, System.Text.Encoding.UTF8);
                xmls.Serialize(memStream, properties);
                memStream.Close();
                control.Value = new System.Xml.XmlDocument();
                control.Value.LoadXml(System.Text.Encoding.UTF8.GetString(memStream.ToArray()));
                control.Update();
                if (control.TransactionKey != Guid.Empty)
                {
                    control.CommitTransaction();
                }
            }
        }
    }
}
